/**
 * 
 */
package com.hs.hbp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.BigNumCal;
import com.hs.hbp.GLOBALCONSTANT;

/**
 * @author shl
 *
 * Dec 9, 2010
 */
public class AbstractAction extends BaseAction {
   private static String SQL = "SELECT * FROM PDMS_POWER ";
   
   private static String ROLE_USER = "SELECT C.ID,C.LOGINID,D.USERNAME    FROM PDMS_ACCOUNTINFO C LEFT JOIN PDMS_USERINFO D ON C.USERID=D.ID WHERE D.ISDEL=0 AND D.ORGID=? AND C.USERSTATUS=0 AND C.ID IN(SELECT RELATIONID FROM PDMS_ROLERELATION A,PDMS_ROLEINFO B WHERE A.ROLEID =B.ID AND B.ROLECODE IN(?))";
   
   private static String USER_POWER_SQL = "SELECT to_char(PARENTID) as pid,to_char(ID)  as id FROM PDMS_POWER P WHERE P.ID IN(SELECT POWERID FROM PDMS_ROLEPOWER WHERE ROLEID IN(SELECT ROLEID FROM PDMS_ROLERELATION WHERE RELATIONID=?)) AND P.LEVELID=3 AND P.ISUSE=0";
//   protected HttpServletRequest getRequest(){
//	   return  ServletActionContext.getRequest(); 
//   } 
//   protected HttpServletResponse getResponse(){
//	   return  ServletActionContext.getResponse(); 
//   } 
    protected Map log(){
	   Map data = new HashMap();
	   return data;
   }
   
//   protected HttpSession getSession(){
//	   return  ServletActionContext.getRequest().getSession(); 
//   } 
   protected void saveLog(HttpServletRequest request){
	   
   }
    protected DBMgr getDbo(HttpServletRequest request){
	   return  DBMgr.getDbUtilInst(request.getSession().getServletContext());
   }
//    protected PdmsAccountinfo getCurrentuser(HttpServletRequest request){
//     	PdmsAccountinfo userInfo = (PdmsAccountinfo)this.getHttpSession(request).getAttribute(GLOBALCONSTANT.USEINSESSION); 
// 	   return  userInfo;
//    }
//    protected Long getCurrentuserOrg(HttpServletRequest request){
//       PdmsAccountinfo accinfo = this.getCurrentuser(request);
// 	   PdmsUserinfo userinfo = accinfo.getUserinfo();
// 	   PdmsOrginfo orginfo = userinfo.getOrginfo();
// 	   Long orgid = -1l;
// 	   if(null != orginfo){
// 		   orgid = orginfo.getId();
// 	   }
// 	   return  orgid;
//    }
   /**
    * 获取当前用户
    * @param request
    * @return
    */
   protected Map getCurrentStr(HttpServletRequest request){
	   Map data = new HashMap();
//	   String menuId = request.getParameter("menuid");
//	   StringBuffer buffer = new StringBuffer("当前位置:");
//	   if(null != menuId && StringUtils.isNotBlank(menuId) && !"null".equals(menuId)){
//		   DictCacheUtil inst = DictCacheUtil.getInst();
//		   Map menuObj = inst.getMenuMap(menuId);
//		   if(menuObj != null){ 
//			   String parentId = menuObj.get("PARENTID") + "";
//			   Map menuParentObj = inst.getMenuMap(parentId);
//			   buffer.append(menuParentObj==null?"":menuParentObj.get("POWERNAME"))
//			   .append("->").append(menuObj==null?"":menuObj.get("POWERNAME"));
//		   }
//	   }
//	   data.put("menuid", menuId);
//	   data.put("menustr", buffer.toString());
//	   this.getHttpSession(request).setAttribute("menuid", menuId);
	   return data;
   }
//   /**
//    * 根据角色代码查询该角色下的用户
//    * @param request
//    * @param roleCodeStr
//    * @return
//    */
//   protected List getUserListByRoleCode(HttpServletRequest request,String roleCodeStr){
//	   List userList = null;
//	   if(StringUtils.isEmpty(roleCodeStr)){
//		   userList = new ArrayList();
//	   }
//	   PdmsAccountinfo accinfo = this.getCurrentuser(request);
//	   PdmsUserinfo userinfo = accinfo.getUserinfo();
//	   PdmsOrginfo orginfo = userinfo.getOrginfo();
//	   Long orgid = -1l;
//	   if(null != orginfo){
//		   orgid = orginfo.getId();
//	   } 
//	 //  userList = this.getDbo(request).queryList(ROLE_USER, new Object[]{roleCodeStr});
//	    
//	   userList = this.getDbo(request).queryList(ROLE_USER, new Object[]{orgid,roleCodeStr});
// 	   return userList;
//	   
//   }
//   /**
//    * 获取session
//    * @param request
//    * @return
//    */
//   protected HttpSession getHttpSession(HttpServletRequest request){
//	   if(null != request)
//		 return   request.getSession();
//	   return null;
//   }
//   protected void putUserInsession(HttpServletRequest request,PdmsAccountinfo accInfo){
//	      this.getHttpSession(request).setAttribute(GLOBALCONSTANT.USEINSESSION, accInfo);
//   }
//   /**
//    * 将登陆用户的信息存入session中
//    * @param request
//    * @return
//    */
//   protected PdmsAccountinfo putUserInsession(HttpServletRequest request){
//	     return  (PdmsAccountinfo)this.getHttpSession(request).getAttribute(GLOBALCONSTANT.USEINSESSION);
//   }
//   protected Map proFinishStatus(PdmsTask task){
//	   Map data  = new HashMap();
//	   int count = 0;
//	   String basic =  task.getBasicflag();
//	   String fjzc =  task.getFjzcflag();
//	   String fjly =  task.getFjlyflag();
//	   String zyjb =  task.getZyjbflag();
//	   String xyjb =  task.getXyjbflag();
//	   String fjgx =  task.getFjgxflag();
//	   String fjzh =  task.getFjzhflag();
//	   
//	   if("1".equals(basic)){
//		   count++;
//	   }
//	   if("1".equals(fjzc)){
//		   count++;
//	   }
//	   if("1".equals(fjly)){
//		   count++;
//	   }
//	   if("1".equals(zyjb)){
//		   count++;
//	   }
//	   if("1".equals(xyjb)){
//		   count++;
//	   }
//	   if("1".equals(fjgx)){
//		   count++;
//	   }
//	   if("1".equals(fjzh)){
//		   count++;
//	   }
//	   data.put("basic", basic);
//	   data.put("fjzc", fjzc);
//	   data.put("fjly", fjly);
//	   data.put("zyjb", zyjb);
//	   data.put("xyjb", xyjb);
//	   data.put("fjgx", fjgx);
//	   data.put("fjzh", fjzh);
// 	   double percent = BigNumCal.div(new Double(count),new Double(7),2);
//	   data.put("percent", percent*1000/10+"%");
//	   return data;
//	   
//   }
//   /**
//    * 记录业务流程中的事件
//    * @param taskid
//    * @param batchid
//    * @param preid
//    * @param eventtype
//    * @param desc
//    */
//   public void recordEvent(Long taskid,Long batchid,Long preid,Long eventtype,String desc){
//	   
//   }
//   /**
//    * 查询获取当前用户的菜单权限并放入session中
//    * @param request
//    */
//   public void putUserPowerInsession(HttpServletRequest request,Long userId){
//	    Long uid = null;
//	    if(null == userId){
//		    PdmsAccountinfo accinfo = this.getCurrentuser( request );
//		    uid = accinfo.getId();
//	    }else{
//	    	uid = userId;
//	    }
//	    if(null == uid){
//	    	System.out.println("没有获取当前用户");
//	    	return;
//	    }
//	    Map roleMap = new HashMap(); 
//    	List powerList = this.getDbo(request).queryList(USER_POWER_SQL, new Object[]{uid});
//    	for(int i = 0;i<powerList.size();i++){
//    		Map power = (Map)powerList.get(i);
//    		String pid = (String)power.get("pid");
//    		String id = (String)power.get("id");
//    		String powername = (String)power.get("POWERNAME");
//    		if(roleMap.get(pid) == null){
//    			roleMap.put(pid, id);
//    		}
//    		roleMap.put(id, id);
//    	}
//    	Map roleMapList = new HashMap();
//    	List roleList = this.getDbo(request).queryList("select ROLECODE from PDMS_ROLEINFO b where b.id in ( select ROLEID from PDMS_ROLERELATION a where A.RELATIONID =?) group by ROLECODE",new Object[]{uid});
//    	for(int j = 0;j<roleList.size();j++){
//    		Map role = (Map)roleList.get(j);
//    		String roleCode = (String)role.get("ROLECODE");
//    		roleMapList.put(roleCode, roleCode);
//    	}
//    	this.getHttpSession(request).setAttribute("USERPOWER_" + uid,roleMap );
//    	this.getHttpSession(request).setAttribute("USERROLE_" + uid,roleMapList );
//	    
//   }
//   /**
//    * 根据用户id获取当前用户权限
//    * @param request
//    * @param userId
//    * @return
//    */
//   protected Map getUserRoleByUserid(HttpServletRequest request,Long userId){
//	   HttpSession session =  this.getHttpSession(request);
//	   Map powerMap = null;
//	   if(null != session){
//		   powerMap = (Map)session.getAttribute("USERPOWER_" + userId);
//	   }
//	   return powerMap;
//   }
//   /**
//    * 根据用户id获取当前用户角色
//    * @param request
//    * @param userId
//    * @return
//    */
//   protected Map getUserRoleCodeByUserid(HttpServletRequest request,Long userId){
//	   HttpSession session =  this.getHttpSession(request);
//	   Map powerMap = null;
//	   if(null != session){
//		   powerMap = (Map)session.getAttribute("USERROLE_" + userId);
//	   }
//	   return powerMap;
//   }
//   /**
//    * 根据任务主键查询审核意见和质检意见
//    * @param request
//    * @param taskId
//    * @return
//    */
//   protected Map getAuditAndQaDesc(HttpServletRequest request,String taskId){
//	   Map desc = new HashMap();
//	   DBMgr dbo = this.getDbo(request);
//	   List auditList = dbo.queryList("select ID,AUDITDESC,AUDITRESULT from PDMS_TASK_AUDIT where id=(select max(id) from PDMS_TASK_AUDIT where TASKID=? and isdelete=1 and STATUS=3)", new Object[]{taskId});
//	   if(auditList.size()>0){
//		   Map data = (Map)auditList.get(0);
//		   String auditdesc = (String)data.get("AUDITDESC");
//		   String auditresult = data.get("AUDITRESULT") +"";
//		   desc.put("auditdesc", auditdesc);
//		   desc.put("auditresult", auditresult);
//	   }else{
//		   desc.put("auditdesc", "");
//		   desc.put("auditresult", "");
//	   }
//	   
//	   List qaList = dbo.queryList("select QADESC,QARESULT,ID from PDMS_QUALITY where taskid=? and ISDELETE=0", new Object[]{taskId});
//	   if(qaList.size()>0){
//		   Map qadata = (Map)auditList.get(0);
//		   String qadescone = (String)qadata.get("QADESC");
//		   String qaresultone = qadata.get("QARESULT") +"";
//		   desc.put("qadescone", qadescone);
//		   desc.put("qaresultone", qaresultone);
//		   
//		   Map qadata2 = (Map)auditList.get(1);
//		   String qadesctwo = (String)qadata2.get("QADESC");
//		   String qaresulttwo = qadata2.get("QARESULT") +"";
//		   desc.put("qadesctwo", qadesctwo);
//		   desc.put("qaresulttwo", qaresulttwo);
//	   }else{
//		   desc.put("qadescone", "");
//		   desc.put("qaresultone", "");
//		   desc.put("qadesctwo", "");
//		   desc.put("qaresulttwo", "");
//	   }
//	   
//	   return desc;
//   }
//   /**
//    * 判断当前用户是否为管理员
//    * @param request
//    * @return
//    */
//   public boolean isAdmin(HttpServletRequest request){
//	    boolean isAdmin= false;
//	    PdmsAccountinfo accinfo = this.getCurrentuser(request);
//		Long uid = accinfo.getId();
//		Map roleMap = this.getUserRoleCodeByUserid(request,uid); 
//	   //是否管理员
//		
//		if(1 == uid || null != roleMap.get(GLOBALCONSTANT.PDMSROLE01) || null != roleMap.get(GLOBALCONSTANT.PDMSROLE02)){
//			isAdmin = true;
//		}
//		return isAdmin;
//   }
}
